Skip to content

Conversation

@claushaas
Copy link

Context
When syncStateValue.reset() is called early (before loadLocal initializes persistence), resetPersistence is still undefined. reset() calls syncStateValue.resetPersistence() unconditionally, which throws:
TypeError: syncStateValue.resetPersistence is not a function (it is undefined).

Steps to reproduce

Call syncState(obs).reset() immediately after creating a synced observable.
In async persistence setups, reset can run before loadLocal finishes.
Root cause
resetPersistence is only assigned inside loadLocal(...). Until then it’s undefined, but reset() calls it without a guard.

Change
Replace the direct call with a safe optional call and fallback.

How this fixes it
Prevents the exception and allows reset() to work even when called before persistence initialization, without changing behavior once resetPersistence exists.

Patch

  • const promise = syncStateValue.resetPersistence();
  • const promise = syncStateValue.resetPersistence?.() ?? Promise.resolve();
    Notes
    This is backward-compatible and no-op when persistence has already been initialized.

If you want me to retry PR creation with a different auth method, tell me what you prefer.

Copilot AI review requested due to automatic review settings January 10, 2026 15:45
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a runtime TypeError that occurs when reset() is called on a synced observable before its persistence layer has been initialized. The issue arises because resetPersistence is only assigned during the asynchronous loadLocal function, but the type system assumes it's always defined.

Changes:

  • Added optional chaining and nullish coalescing to safely call resetPersistence?.() in the reset() function
  • Falls back to Promise.resolve() when resetPersistence is undefined

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant